package edu.stanford.bmir.protege.web.server.stkosutil.enricher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.semanticweb.owlapi.model.EntityType;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;

import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.UtilConstants;
import edu.stanford.bmir.protege.web.client.rpc.stkosutil.enricher.CandidateEntity;
import edu.stanford.bmir.protege.web.server.change.ChangeGenerationContext;
import edu.stanford.bmir.protege.web.server.change.ChangeListGenerator;
import edu.stanford.bmir.protege.web.server.change.OntologyChangeList;
import edu.stanford.bmir.protege.web.server.owlapi.OWLAPIProject;
import edu.stanford.bmir.protege.web.server.owlapi.RenameMap;
import edu.stanford.bmir.protege.web.shared.DataFactory;

public class CandidateClassesChangeGenerator implements ChangeListGenerator<Set<OWLClass>>{
	private ArrayList<CandidateClassHolder> alCandidateClassHolders = null;
	private HashMap<OWLClass, String> _hFreshClassList = null;
	public CandidateClassesChangeGenerator(ArrayList<CandidateClassHolder> candidateClassHolders){
		this.alCandidateClassHolders = candidateClassHolders;
	}
	public OntologyChangeList<Set<OWLClass>> generateChanges(OWLAPIProject project, ChangeGenerationContext context) {
		HashSet<OWLClass> sFreshClassList = new HashSet<OWLClass>();
		_hFreshClassList = new HashMap<OWLClass, String>();
		OntologyChangeList.Builder<Set<OWLClass>> builder = new OntologyChangeList.Builder<Set<OWLClass>>();
		for (CandidateClassHolder candidateHolder: alCandidateClassHolders){
			OWLClass superClass = candidateHolder.getOwlClass();
			for (CandidateEntity candidateChildClass: candidateHolder.getCandidateChildClassList()){
				OWLClass freshClass = DataFactory.getFreshOWLEntity(EntityType.CLASS, candidateChildClass.getBrowserText());	
				OWLDataFactory df = DataFactory.get();
				builder.addAxiom(project.getRootOntology(), df.getOWLDeclarationAxiom(freshClass));
				builder.addAxiom(project.getRootOntology(), df.getOWLSubClassOfAxiom(freshClass, superClass));
//				String annotaionValue= UtilConstants.FLAG_ENRICHED + ": Y" + ";" + UtilConstants.FLAG_FROM + ": " + candidateChildClass.getSourceLabel(); 
//	 			OWLAnnotation commentAnno = df.getOWLAnnotation(
//	 					 df.getRDFSComment(),
//	 					 df.getOWLLiteral(annotaionValue, "en"));
//	 			OWLAxiom ax = df.getOWLAnnotationAssertionAxiom(
//	 					freshClass.getIRI(), commentAnno);
//	 			builder.addAxiom(project.getRootOntology(), ax);
	 			sFreshClassList.add(freshClass);
	 			this._hFreshClassList.put(freshClass, candidateChildClass.getSourceLabel());
	 			
			} 
		}
		return builder.build(sFreshClassList);
	}
	
	public Set<OWLClass> getRenamedResult(Set<OWLClass> result, RenameMap renameMap) {
	        return renameMap.getRenamedEntities(result);
	}    
	public HashMap<OWLClass, String> getFresshClassList(){
		return this._hFreshClassList;
	}
}
